package com.Interceptor;

import com.util.ConstantUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UserSecurityInterceptor implements HandlerInterceptor {

    private  List<String> fiterlist = Arrays.asList("helpcenter","index","loginSubmit","findPassword","register","kaptcha","promotionlist","forgotpasswordone","forgotpasswordtwo","forgotpasswordthree");

    private  List<String> usercenterPagefiter = Arrays.asList("questionDialog","questionWrite","systemMessageReceive","banklist");



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(request.getPathInfo());
        System.out.println(request.getQueryString());
        System.out.println(request.getRequestURI());
        String requestMethod =  request.getRequestURI();

        for (String item:fiterlist){
            if(requestMethod.indexOf(item) !=-1 ){
                return true;
            }
        }
        Object obj = request.getSession().getAttribute(ConstantUtil.loginSession);
        if(obj == null){
            response.sendRedirect("/index");
            return false;
        }

        request.setAttribute("showGames",false);

        for (String item :usercenterPagefiter){
            if(requestMethod.indexOf(item) !=-1 ) {
                request.setAttribute("showGames",true);
            }
        }
        return true;
    }

    @Override  
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }  
  
    @Override  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {  
  
    }  
}